<template>
  <view class="login-container">
    <uni-icons type="contact-filled" size="100"></uni-icons>
    <button class="login-btn"  @click="getUserProfile">一键登录</button>
    <text>登录后尽享更多权益</text>
  </view>
</template>
<script>
  import {mapMutations,mapState} from 'vuex'
  export default {
    name:"my-login",
    data() {
      return {
      };
    },
    computed:{
      ...mapState('m_address',['redirectInfo'])
    },
    methods:{
      ...mapMutations('m_address',['updateUserInfo','saveToken','updateRedirectInfo']),
      getUserProfile(){
        uni.getUserProfile({
          lang: 'zh_CN',
          desc: '用于完善会员资料',
          success:(user)=>{
          					console.log('11', user);
                    console.log('22',user.userInfo)
                    //把获取到的用户信息存到vuex中并持久化处理
                    this.updateUserInfo(user.userInfo)
                    this.getToken(user)
                    //登录成功后跳转购物车
                   this.navigeteBack()
          				},
          fail:(res)=>{
            uni.$showMsg('您取消了登录授权')
               }
        })
        
      },
      navigeteBack(){
        //判断是不是因为在购物车点击结算没登录跳过来的
        if(this.redirectInfo && this.redirectInfo.openType === 'switchTab'){
          uni.switchTab({
            url:this.redirectInfo.from,
            complete: () => {
              this.updateRedirectInfo(null)
            }
          })
        }
      },
      //把获取到的用户信息传过来
      getToken(user){
        //通过uni.login来获取code
      uni.login({
          success: async (res) => {
            console.log(res)
            //获取到code
            console.log('11',res.code)
            console.log('22',user)
            //新建query存post请求需要的参数
            const query = {
              code:res.code,
              encryptedData:user.encryptedData,
              iv:user.iv,
              rawData:user.rawData,
              signature:user.signature
            }
            console.log(query)
            //发起post请求用户获取token
        const result =   await uni.$http.post('/api/public/v1/users/wxlogin',query)
        //把获取到的token存到vuex中并持久化
        // this.saveToken(query.code)
        this.saveToken("Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjIzLCJpYXQiOjE1NjQ3MzAwNzksImV4cCI6MTAwMTU2NDczMDA3OH0.YPt-XeLnjV-_1ITaXGY2FhxmCe4NvXuRnRB8OMCfnPo")
        uni.switchTab({
          url:'/pages/cart/cart'
        })
          },
          fail: (error) => {
            console.log(error)
            return uni.$showMsg('登录失败')
          }
        })
      }
    }
  }
</script>

<style lang="scss">
.login-container{
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 750rpx;
  background-color: #f8f8f8;
  position: relative;
  overflow: hidden;
  &::after{
    content: '';
    display: block;
    position: absolute;
    bottom: 0;
    left: 0;
    transform: translateY(50%);
    background-color: aqua;
    width: 100%;
    height: 50px;
    border-radius: 100%;
    background-color: #f5f5f5;  
  }
  uni-icons{

  }
  .login-btn{
    width: 90%;
    background-color: #C00000;
    border-radius: 100px;
    margin: 15px 0;
    color: #fff;
  }
  text{
    font-size: 12px;
    color: gray;
  }
}
</style>